﻿using Microsoft.Phone.Controls;
using System.Windows;
using System.Diagnostics;
using System;
using Microsoft.Phone.Tasks;
using Microsoft.Phone.Shell;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Controls;

namespace VisualStudioAchievements
{
    public partial class UserDetail : PhoneApplicationPage
    {
        public UserDetail()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            if (App.MainPageDispatcher == null)
                App.MainPageDispatcher = this.Dispatcher;

            try
            {
                string ninerString = NavigationContext.QueryString["niner"];
                NinerReader reader = new NinerReader();
                reader.GetNiner(ninerString, true, niner =>
                {
                    BindingPoint.Instance.CurrentNiner = niner;
                });
            }
            catch
            {
                Debug.WriteLine("No parameter. This means that it was not launched from a tile.");
            }

            base.OnNavigatedTo(e);
        }

        private void btnShare_Click(object sender, EventArgs e)
        {
            var shareTask = new ShareStatusTask();
            shareTask.Status = string.Format("@ch9 VS Achievements: {0} currently has {1} points. #vsawp", 
                BindingPoint.Instance.CurrentNiner.Name, BindingPoint.Instance.CurrentNiner.Points);
            shareTask.Show();
        }

        private void btnPin_Click(object sender, EventArgs e)
        {
            Niner niner = BindingPoint.Instance.CurrentNiner;

            Uri shellUri = new Uri("/UserDetail.xaml?niner=" + niner.Alias, UriKind.Relative);
            ShellTile.Create(shellUri, new StandardTileData()
            {
                Title = niner.Alias,
                BackgroundImage = niner.Avatar
            });
        }

        private void stkUserPanel_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            if (MessageBox.Show("Open user profile in Internet Explorer?", "User Profile", 
                MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                var webTask = new WebBrowserTask();
                webTask.Uri = new Uri(string.Format(@"http://channel9.msdn.com/Niners/{0}", 
                    BindingPoint.Instance.CurrentNiner.Alias));
                webTask.Show();
            }
        }

        private void WebBrowser_Navigating(object sender, NavigatingEventArgs e)
        {
            e.Cancel = true;

            WebBrowserTask task = new WebBrowserTask();
            task.Uri = e.Uri;
            task.Show();
        }

        private void WebBrowser_Loaded(object sender, RoutedEventArgs e)
        {
            var lookupBorder = Util.FindChildInTree(sender as WebBrowser, 5) as Border;

            lookupBorder.ManipulationCompleted += new 
                EventHandler<System.Windows.Input.ManipulationCompletedEventArgs>(border_ManipulationCompleted);
            lookupBorder.ManipulationDelta += new 
                EventHandler<System.Windows.Input.ManipulationDeltaEventArgs>(border_ManipulationDelta);
        }

        void border_ManipulationDelta(object sender, System.Windows.Input.ManipulationDeltaEventArgs e)
        {
            e.Handled = true;
        }

        void border_ManipulationCompleted(object sender, System.Windows.Input.ManipulationCompletedEventArgs e)
        {
            e.Handled = true;
        }
    }
}